package com.mea.client;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.microedition.io.StreamConnection;

import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.kuix.widget.Screen;
import org.kalmeo.util.frame.Frame;

public class ServerFoundFrame implements Frame {
	
	StreamConnection stream = null;
    OutputStream out = null;

	public ServerFoundFrame(StreamConnection stream2, OutputStream out2) {
		stream = stream2;
		out = out2;
	}

	public void onAdded() {
		// Load the content from the XML file with Kuix.loadScreen static method
	    Screen screen = Kuix.loadScreen("serverfound.xml", null);

	    // Set the application current screen  
	    screen.setCurrent();
		
	}

	public boolean onMessage(Object identifier, Object[] arguments) {
		if ("play".equals(identifier)) {
	        // signals play
			send(new String("play"));
	        return false;
	    }
		if ("pause".equals(identifier)) {
	        // signals pause
			send(new String("pause"));
	        return false;
	    }
		if ("about".equals(identifier)) {
	        // display a popup message
	        Kuix.alert("This Helloworld is powered by Kuix", KuixConstants.ALERT_OK);
	        return false;
	    }
	    if ("exitConfirm".equals(identifier)) {
	        // display a popup message
	        Kuix.alert("Are you sure you want to exit?", KuixConstants.ALERT_YES | KuixConstants.ALERT_NO, "exit", null);
	        return false;
	    }
	    if ("exit".equals(identifier)) {
	        // get the midlet instance to invoke the Destroy() method
	        HelloWorld.getDefault().destroyImpl();
	        //if the event has been processed, we return 'false' to avoid event forwarding to other frames
	        return false;
	    }

	    // return "true" makes the FramHandler to forward the message to the next frame in the stack 
	    return true;
	}

	public void onRemoved() {
		try {
            stream.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
		
	}
	
	private void send(String message) {
        try {

            byte data[] = message.getBytes("UTF-8");

        	//box.setString(box.getString() + "\nSending " + data.length + "bytes");
                //debug.setText(debug.getText() + "\nSending " + data.length + "bytes");
            try {
                out.write(data);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } catch(UnsupportedEncodingException e) {
        	//box.setString(box.getString() + "Unsupported Encoding");
                //debug.setText(debug.getText() + "Unsupported Encoding");
        }

         try {
            out.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        //box.setString(box.getString() + "\nDONE");
        //debug.setText(debug.getText() + "\nDONE");
    }

}
